package com.zucc.eye.dto;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zucc.eye.entity.*;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description issue
 * @author zhengkai.blog.csdn.net
 * @date 2022-02-09
 */
//@Entity
@Builder
@AllArgsConstructor
@Data
@Table(name="issue")
@ApiModel("issue")
@Slf4j
@ToString
public class IssueDto implements Serializable {

   public   IssueDto(Issue issue){
        BeanUtils.copyProperties(issue,this);
       //issue.setAssignees( JSONObject.toJSONString(assignees));
       //issue.setLabels( JSONObject.toJSONString(labels));
       //issue.setReactions( JSONObject.toJSONString(reactions));
       //issue.setUser( JSONObject.toJSONString(user));

       List<String> list = JSONObject.parseArray(issue.getAssignees(), String.class);
       Assignee assignee = JSONObject.parseObject(issue.getAssignee(), Assignee.class);
       List<Assignee> assignees = JSONObject.parseArray(issue.getAssignees(), Assignee.class);
       log.info("list {}",list);
       log.info("assignees {}",assignees);
       log.info("assignee {}",assignee);
       setAssignee(assignee);
       //s
       //setAssignees(JSONObject.parseArray(issue.getAssignees(), String.class));
       setAssignees(assignees);
       //String labels1 = issue.getLabels();
       JSONArray objects = JSONObject.parseArray(issue.getLabels());
       //JSONArray 全部转化
       //Object o1 = objects.get(0);
       List<Label> labels = objects.stream().map(o -> {
           JSONObject jsonObject = (JSONObject) o;
           //JSONObject.parseObject()
           Label label = JSONObject.parseObject(String.valueOf(jsonObject), Label.class);

           //从json获取的是 def
           //default
           Boolean aDefault = jsonObject.getBoolean("default");
           label.setDefault(aDefault);
           return label;
       }).collect(Collectors.toList());
       //objects.forEach(studentJson -> {
       //    JSONObject jsonObject = (JSONObject)studentJson;
       //    //JSONObject.parseObject()
       //    Label label = JSONObject.parseObject(String.valueOf(jsonObject), Label.class);
       //
       //    //从json获取的是 def
       //    //default
       //    Boolean aDefault = jsonObject.getBoolean("default");
       //    label.setDefault(aDefault);
       //    //Student student = new Student(jsonObject.getString("name"), jsonObject.getInteger("age"), jsonObject.getString("gender"));
       //    //tmpList.add(student);
       //});

       //objects.forEach(o->{
       //    JSONObject.parseObject(o,Label.class);
       //    //JSONObject.to
       //    //JSONObject.parse
       //    //T t = JSONObject.toJavaObject(o, Label.class);
       //    //JSON.obj
       //});
       //List<Label> labels = JSONObject.parseArray(issue.getLabels(), Label.class);
       //setLabels(  JSONObject.parseArray(issue.getLabels(), String.class));
       log.info("labels {}",labels);
       setLabels(  labels);
       Reactions reactions = JSONObject.parseObject(issue.getReactions(), Reactions.class);
       log.info("reactions {}",reactions);
       setReactions(  JSONObject.parseObject(issue.getReactions(), Reactions.class));
       setUser( JSONObject.parseObject(issue.getUser(), User.class));
       log.info("this {} ",this);
       //
       //Reactions reactions = JSONObject.parseObject(issue.getReactions(), Reactions.class);
       //User user = JSONObject.parseObject(comment.getReactions(), User.class);
       //setReactions(reactions);
       //setUser(user);
    }

    public Issue toEntity(){
        //Repo repo = new Repo();
        Issue issue = new Issue();
        BeanUtils.copyProperties(this,issue);
        //一个实体没办法复制吧
        //String stringOwner = JSONObject.toJSONString(owner);
        //owner
        //assignees
        //labels
        //reactions
        //user
        issue.setAssignees( JSONObject.toJSONString(assignees));
        //JSONObject.parseObject(repo,Repo.class);
        //license
        //String stringOwner = JSONObject.toJSONString(owner);
        //owner
        //labels
        //JSONObject.parseArray(labels,Label.class);
        issue.setLabels( JSONObject.toJSONString(labels));
        issue.setReactions( JSONObject.toJSONString(reactions));
        issue.setUser( JSONObject.toJSONString(user));
        return issue;
    }

    private static final long serialVersionUID = 1L;

    @Id
    @ApiModelProperty("id")
    //@GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    /**
    * user_name
    */
    @ApiModelProperty("user_name")
    @Column(name="user_name")
    private String userName;

    /**
    * user_url
    */
    @ApiModelProperty("user_url")
    @Column(name="user_url")
    private String userUrl;

    /**
    * user_avatar
    */
    @ApiModelProperty("user_avatar")
    @Column(name="user_avatar")
    private String userAvatar;

    /**
    * date
    */
    @ApiModelProperty("date")
    @Column(name="date")
    private Date date;

    /**
    * url
    */
    @ApiModelProperty("url")
    @Column(name="url")
    private String url;

    /**
    * body
    */
    @ApiModelProperty("body")
    @Column(name="body")
    private String body;

    /**
    * repository_url
    */
    @ApiModelProperty("repository_url")
    @Column(name="repository_url")
    private String repositoryUrl;

    /**
    * labels_url
    */
    @ApiModelProperty("labels_url")
    @Column(name="labels_url")
    private String labelsUrl;
    //private List<String> labelsUrl
    //private String labelsUrl;

    /**
    * comments_url
    */
    @ApiModelProperty("comments_url")
    @Column(name="comments_url")
    private String commentsUrl;

    /**
    * events_url
    */
    @ApiModelProperty("events_url")
    @Column(name="events_url")
    private String eventsUrl;

    /**
    * html_url
    */
    @ApiModelProperty("html_url")
    @Column(name="html_url")
    private String htmlUrl;

    /**
    * node_id
    */
    @ApiModelProperty("node_id")
    @Column(name="node_id")
    private String nodeId;

    /**
    * number
    */
    @ApiModelProperty("number")
    @Column(name="number")
    private Integer number;

    /**
    * title
    */
    @ApiModelProperty("title")
    @Column(name="title")
    private String title;

    /**
    * user
    */
    @ApiModelProperty("user")
    @Column(name="user")
    //private String user;
    private User user;

    /**
    * state
    */
    @ApiModelProperty("state")
    @Column(name="state")
    private String state;

    /**
    * labels
    */
    @ApiModelProperty("labels")
    @Column(name="labels")
    //private String labels;
    //private List<String> labels;
    private List<Label> labels;

    /**
    * locked
    */
    @ApiModelProperty("locked")
    @Column(name="locked")
    private Integer locked;

    /**
    * assignee
    */
    @ApiModelProperty("assignee")
    @Column(name="assignee")
    //private String assignee;
    private Assignee assignee;
    //private List<String > assignee;

    /**
    * assignees
    */
    @ApiModelProperty("assignees")
    @Column(name="assignees")
    //private List<String > assignees;
    private List<Assignee > assignees;

    /**
    * milestone
    */
    @ApiModelProperty("milestone")
    @Column(name="milestone")
    private String milestone;

    /**
    * comments
    */
    @ApiModelProperty("comments")
    @Column(name="comments")
    private Integer comments;

    /**
    * created_at
    */
    @ApiModelProperty("created_at")
    @Column(name="created_at")
    private Date createdAt;

    /**
    * updated_at
    */
    @ApiModelProperty("updated_at")
    @Column(name="updated_at")
    private Date updatedAt;

    /**
    * closed_at
    */
    @ApiModelProperty("closed_at")
    @Column(name="closed_at")
    private Date closedAt;

    /**
    * author_association
    */
    @ApiModelProperty("author_association")
    @Column(name="author_association")
    private String authorAssociation;

    /**
    * active_lock_reason
    */
    @ApiModelProperty("active_lock_reason")
    @Column(name="active_lock_reason")
    private String activeLockReason;

    /**
    * reactions
    */
    @ApiModelProperty("reactions")
    @Column(name="reactions")
    //private String reactions;
    private Reactions reactions;

    /**
    * timeline_url
    */
    @ApiModelProperty("timeline_url")
    @Column(name="timeline_url")
    private String timelineUrl;

    /**
    * performed_via_github_app
    */
    @ApiModelProperty("performed_via_github_app")
    @Column(name="performed_via_github_app")
    private Integer performedViaGithubApp;

    public IssueDto() {
    }

}